<template>
    <view :style="{ '--color': color, '--back-color': $common.hexToRgba(color, 0.1), '--back-colors': $common.hexToRgba(color, 0.8) }">
        <view class="Wrapper" :style="windowHeigth">
            <view class="Top">
                <view class="navBar" :style="navBarHeight">
                    <view class="barBox" :style="navStyle">
                        <view class="Search">
                            <view class="iconfont icon-sousuokuang1 color_cc size30"></view>
                            <input type="text" v-model="search" class="searchTest" placeholder="搜索" @confirm="SearchBtn" />
                            <view class="SearchBtn" @click="SearchBtn">搜索</view>
                        </view>
                    </view>
                </view>
                <view class="oneLanBox">
                    <scroll-view scroll-x="true" class="Onescrollview nowrap">
                        <view class="list" @click="changeNav(index)" :class="{ Active: current == index }" v-for="(item, index) in onceLevel" :key="index">
                            <view class="IconImg">
                                <image :src="$imgUrls(item.classify_img)" mode="aspectFill" class="classify_img"></image>
                            </view>
                            <view class="classify_name">{{ item.classify_name }}</view>
                        </view>
                    </scroll-view>
                    <view class="ShowLAn" @click="show = true">
                        <view class="Unfold">展开</view>
                        <view class="iconfont icon-zhankai color_33 26"></view>
                    </view>
                </view>
            </view>

            <!--  分类 -->
            <view class="ClasBox" :style="ClasBoxStyle">
                <view class="Nav" v-if="onceLevel[current].list">
                    <view class="listNav ellipsis" @click="Navbtn(item)" v-for="(item, index) in onceLevel[current].list" :key="index" :class="{ active: item.classify_id == classify_id }">
                        {{ item.classify_name }}
                    </view>
                </view>

                <view class="content" :style="ClasBoxStyle">
                    <view class="Screening">
                        <view class="screenList" @click="screenBtn(item)" v-for="(item, index) in screenData" :key="index">
                            <text class="scrTxt" :class="{ active: type == item.type }">{{ item.name }}</text>
                            <view class="iconeniBox" v-if="item.type">
                                <view class="iconfont icon-paixu-shang iconeni size20" :class="{ active: type == item.type && item.state == 1 }"></view>
                                <view class="iconfont icon-paixu-xia iconeni size20" :class="{ active: type == item.type && item.state == 2 }"></view>
                            </view>
                        </view>
                    </view>

                    <view class="dataBox" :style="contentStyle">
                        <scroll-view scroll-y="true" :scroll-top="scrollTop" @scroll="scroll" :lower-threshold="100" @scrolltolower="scrolltolower" class="rows" :style="contentStyle">
                            <view class="List" @click.stop="toGoodsDetail(item)" v-for="(item, index) in pageData" :key="index">
                                <image :src="$imgUrls(item.img)" mode="aspectFill" class="Img"></image>
                                <view class="dataContent">
                                    <view class="d-flex f-column">
                                        <text class="name ellipsis_2">{{ item.name }}</text>
                                        <text class="info ellipsis" v-if="item.info">{{ item.info }}</text>
                                    </view>
                                    <view class="row a-center">
                                        <view class="d-flex a-center black m-right10">
                                            <text class="size20">￥</text>
                                            <text class="size28 bold500">{{ item.price }}</text>
                                        </view>
                                        <text class="color_ff4 h6">{{ item.profitTitle }}</text>
                                        <view class="rob">抢</view>
                                    </view>
                                </view>
                            </view>
                            <emptys v-if="!pageData.length"></emptys>
                        </scroll-view>
                    </view>
                </view>
            </view>
            <tabBar :tab="18"></tabBar>
        </view>

        <view class="popUpBox" :style="popStyle" v-show="show">
            <view class="pinpong">
                <view class="pipnine">
                    <text class="black size28 bold500">全部分类</text>
                    <view class="pinlis">
                        <view class="onlist" @click="changeNav(index)" :class="{ Active: current == index }" v-for="(item, index) in onceLevel" :key="index">
                            <view class="onImg">
                                <image :src="$imgUrls(item.classify_img)" mode="aspectFill" class="wh"></image>
                            </view>
                            <view class="classify_name">{{ item.classify_name }}</view>
                        </view>
                    </view>
                </view>
                <view class="hideView" @click.stop="show = false">
                    <text class="color_99 h6">点击收起</text>
                    <view class="iconfont icon-xiangshang color_99 size20 m-left10"></view>
                </view>
            </view>
        </view>
    </view>
</template>

<script>
    import { mapState } from 'vuex';
    export default {
        data() {
            return {
                search: '',
                cateType: 0, // 为1显示课程分类
                onceLevel: [],
                classify_id: '',
                current: 0,
                screenData: [
                    {
                        name: '默认',
                        state: 1,
                        type: ''
                    },
                    {
                        name: '佣金',
                        type: 6,
                        state: 1
                    },
                    {
                        name: '价格',
                        type: 1,
                        state: 1
                    }
                ],
                type: '', // 类型
                state: 2, // 排序方式（除 type=5外） 1正序 2倒序
                page: 1,
                pageNum: 10,
                brand_id: 0,
                getMore: true,
                pageData: [],
                scrollTop: 0,
                scrollTopCope: 0,
                show: false
            };
        },
        onLoad() {
            this.allUpList();
        },
        methods: {
            allUpList() {
                this.$http
                    .post({
                        url: '/singleSaleApi/allUpList',
                        data: {
                            classify_cate: this.cateType
                        }
                    })
                    .then((res) => {
                        if (res.errcode != 100) return this.$Toast(res.msg);
                        const result = res.structure || [];
                        result.forEach((item) => {
                            if (!item.list) item.list = [];
                            const objList = {
                                classify_id: item.classify_id,
                                classify_name: '全部'
                            };
                            item.list.unshift(objList);
                        });
                        this.onceLevel = result;
                        this.classify_id = this.onceLevel[0].list[0].classify_id;
                        this.sortClassGoods();
                    });
            },
            sortClassGoods() {
                this.$http
                    .post({
                        url: '/singleSaleApi/sortClassGoods',
                        data: {
                            page: this.page,
                            pageNum: this.pageNum,
                            classify_id: this.classify_id,
                            goods_cate: this.cateType,
                            mode: 1,
                            brand_id: this.brand_id,
                            type: this.type,
                            state: this.state,
                            search: this.search
                        }
                    })
                    .then((res) => {
                        if (res.code != 100) return this.$Toast(res.msg);
                        if (this.page == 1) this.scrollTop = 0;
                        const result = res.data.DataArr || [];
                        this.page == 1 ? (this.pageData = result) : (this.pageData = [...this.pageData, ...result]);
                        if (result.length < this.pageNum) this.getMore = false;
                    });
            },
            changeNav(index) {
                this.current = index;

                this.classify_id = this.onceLevel[index].list[0].classify_id;
                this.init();
            },
            Navbtn(item) {
                this.classify_id = item.classify_id;

                this.init();
            },
            screenBtn(item) {
                this.type = item.type;

                if (this.state == 2) {
                    item.state = 1;
                    this.state = 1;
                } else {
                    item.state = 2;
                    this.state = 2;
                }

                this.init();
            },
            init() {
                this.page = 1;
                this.getMore = true;
                this.scrollTop = this.scrollTopCope;
                this.show = false;
                this.sortClassGoods();
            },
            scrolltolower() {
                if (!this.getMore) return;
                this.page++;
                this.sortClassGoods();
            },
            scroll(e) {
                let scrollTop = Math.floor(e.detail.scrollTop);
                this.scrollTopCope = scrollTop;
            },
            SearchBtn() {
                if (!this.search) return;
                this.init();
            },
            async toGoodsDetail(item) {
                if (item.goods_cate == 1) return this.$toCourses(item.goods_id);
                // if (item.goods_cate == 6) {
                //     if (!this.vid) return (this.showModal = true);
                //     this.ShareBtn(item)
                //     return;
                // }
                this.toGoods(item.goods_id);
            }
        },
        computed: {
            ...mapState(['totalHeight', 'System', 'color', 'navHeight', 'navWidth']),
            windowHeigth() {
                let H = this.System.windowHeight;
                return `height: ${H}px`;
            },
            navBarHeight() {
                return `height: ${this.totalHeight}px`;
            },
            navStyle() {
                return `width: ${this.navWidth}px; height: ${this.navHeight}px;`;
            },
            ClasBoxStyle() {
                let ovee = this.System.screenHeight - this.System.safeArea.bottom;
                let H = this.System.windowHeight - this.totalHeight - 100 + 10 - 50 - ovee;
                return `height: ${H}px`;
            },
            contentStyle() {
                let ovee = this.System.screenHeight - this.System.safeArea.bottom;
                let H = this.System.windowHeight - this.totalHeight - 100 + 10 - 33 - 50 - ovee;
                return `height: ${H}px`;
            },
            popStyle() {
                let H = this.System.windowHeight - this.totalHeight;
                return `height: ${H}px`;
            }
        }
    };
</script>

<style lang="scss" scoped>
    .popUpBox {
        width: 100%;
        position: fixed;
        bottom: 0;
        left: 0;
        z-index: 100000;
        background-color: rgba(0, 0, 0, 0.5);

        .pinpong {
            .pipnine {
                width: 100%;
                background-color: #ffffff;
                padding: 24rpx;
                display: flex;
                flex-direction: column;

                .pinlis {
                    width: 100%;
                    display: grid;
                    grid-template-columns: repeat(5, 1fr);
                    row-gap: 30rpx;
                    margin-top: 20rpx;
                    .onlist {
                        display: flex;
                        flex-direction: column;
                        align-items: center;
                        .onImg {
                            width: 88rpx;
                            height: 88rpx;
                            border-radius: 20rpx;
                            overflow: hidden;
                            border: 2px solid #fff;
                        }
                        .classify_name {
                            padding: 4rpx 14rpx;
                            border-radius: 20rpx;
                            color: #333;
                            margin-top: 10rpx;
                            font-size: 22rpx;
                        }
                    }
                    .Active {
                        .onImg {
                            border: 2px solid var(--color);
                        }
                        .classify_name {
                            color: #fff;
                            background-color: var(--color);
                        }
                    }
                }
            }
            .hideView {
                width: 100%;
                height: 80rpx;
                background-color: #ffffff;
                border-top: 1px solid #f1f1f1;
                border-radius: 0 0 10rpx 10rpx;
                display: flex;
                justify-content: center;
                align-items: center;
            }
        }
    }
    .Wrapper {
        width: 100%;
        box-sizing: border-box;
    }
    .Top {
        background-image: linear-gradient(120deg, var(--back-color) 0%, var(--color) 80%, var(--color) 100%);
    }
    .navBar {
        width: 100%;
        display: flex;
        flex-direction: column;
        justify-content: flex-end;
        position: relative;
        z-index: 2000000;
        .barBox {
            display: flex;
            justify-content: center;
            align-items: center;
            .Search {
                width: 90%;
                height: 70rpx;
                border-radius: 35rpx;
                border: 1px solid #f1f1f1;
                background-color: #ffffff;
                display: flex;
                align-items: center;
                padding-left: 24rpx;
                .searchTest {
                    height: 100%;
                    flex: 1;
                    padding: 0 20rpx;
                    font-size: 26rpx;
                    color: #000;
                }
                .SearchBtn {
                    height: 100%;
                    padding: 0 24rpx;
                    display: flex;
                    justify-content: center;
                    align-items: center;
                    color: var(--color);
                    font-size: 26rpx;
                }
            }
        }
    }
    .oneLanBox {
        width: 100%;
        height: 200rpx;
        display: flex;
        align-items: center;
        position: relative;
        .Onescrollview {
            width: 100%;
            height: 160rpx;
            padding-right: 60rpx;

            .list {
                display: inline-block;
                margin-right: 10rpx;
                margin-left: 10rpx;
                .IconImg {
                    width: 88rpx;
                    height: 88rpx;
                    border-radius: 20rpx;
                    overflow: hidden;
                    border: 1px solid #fff;
                    margin: 0 auto;
                    .classify_img {
                        width: 100%;
                        height: 100%;
                    }
                }
                .classify_name {
                    height: 40rpx;
                    line-height: 40rpx;
                    padding: 0 20rpx;
                    border-radius: 20rpx;
                    text-align: center;

                    color: #333;
                    font-size: 22rpx;
                    margin-top: 10rpx;
                }
            }
            .Active {
                .IconImg {
                    border: 1px solid var(--color);
                }
                .classify_name {
                    background-color: var(--color);
                    color: #fff;
                }
            }
        }
        .ShowLAn {
            width: 80rpx;
            height: 160rpx;
            background-color: var(--color);
            position: absolute;
            top: 20rpx;
            right: 0;
            z-index: 10;
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            .Unfold {
                color: #333;
                font-size: 26rpx;
                writing-mode: vertical-rl;
                letter-spacing: 10rpx;
            }
        }
    }
    .ClasBox {
        width: 100%;
        background-color: #fff;
        margin-top: -20rpx;
        position: relative;
        z-index: 20;
        border-radius: 18rpx 18rpx 0 0;
        display: flex;
        overflow: hidden;
        .Nav {
            width: 190rpx;
            height: 100%;
            overflow-y: scroll;
            display: flex;
            flex-direction: column;
            text-align: center;
            background-color: #f5f5f5;
            .listNav {
                width: 100%;
                height: 90rpx;
                position: relative;
                display: flex;
                align-items: center;
                justify-content: center;
                color: #666;
                font-size: 28rpx;
            }
            .active {
                color: var(--color) !important;
                position: relative;
                font-weight: 500;
                background-color: #ffffff;
            }
            .active::after {
                content: '';
                width: 6rpx;
                height: 50%;
                background-color: var(--color);
                position: absolute;
                top: 50%;
                left: 0;
                transform: translateY(-50%);
            }
        }
        .content {
            flex: 1;
            padding: 0 10rpx;
            display: flex;
            flex-direction: column;
            .Screening {
                width: 100%;
                height: 60rpx;
                border-bottom: 1px solid #f1f1f1;
                display: flex;
                align-items: center;
                .screenList {
                    flex: 1;
                    height: 100%;
                    display: flex;
                    align-items: center;
                    display: flex;
                    justify-content: center;
                    align-items: center;
                    .scrTxt {
                        color: #999;
                        font-size: 24rpx;
                    }
                    .active {
                        color: var(--color);
                    }
                    .iconeniBox {
                        display: flex;
                        flex-direction: column;
                        justify-content: space-between;
                        margin-left: 4rpx;
                        .iconeni {
                            scale: 0.6;
                            line-height: 14rpx;
                            color: #999;
                        }
                        .active {
                            color: var(--color);
                        }
                    }
                }
            }
            .dataBox {
                width: 100%;
                box-sizing: border-box;
                .List {
                    width: 100%;
                    padding: 20rpx 0;
                    display: flex;
                    align-items: center;
                    border-bottom: 1px solid #f1f1f1;
                    .Img {
                        width: 180rpx;
                        height: 180rpx;
                        border-radius: 10rpx;
                        margin-right: 10rpx;
                    }
                    .dataContent {
                        flex: 1;
                        height: 180rpx;
                        display: flex;
                        flex-direction: column;
                        justify-content: space-between;
                        .name {
                            color: #000;
                            font-size: 28rpx;
                            font-weight: 500;
                        }
                        .info {
                            color: #999;
                            font-size: 24rpx;
                        }
                        .rob {
                            padding: 4rpx 24rpx;
                            border-radius: 20rpx;
                            color: #fff;
                            font-size: 24rpx;
                            background-color: var(--color);
                            margin-left: auto;
                        }
                    }
                }
            }
        }
    }
</style>
